package icu.xblogs.report.service.impl;

import icu.xblogs.report.domain.AttendanceRecord;
import icu.xblogs.report.domain.Employee;
import icu.xblogs.report.mapper.AttendanceRecordMapper;
import icu.xblogs.report.mapper.EmployeeMapper;
import icu.xblogs.report.plugin.Page;
import icu.xblogs.report.service.AttendanceRecordService;
import icu.xblogs.report.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;
@Service("attendanceRecordService")
public class AttendanceRecordServiceImpl implements AttendanceRecordService {
    @Autowired
    AttendanceRecordMapper attendanceRecordMapper;
    @Autowired
    EmployeeMapper employeeMapper;

    @Override
    public List<Map<String, String>> getDayList(String day) {
        List<AttendanceRecord> attendanceRecords = attendanceRecordMapper.selectDay(day);
        ArrayList<Map<String, String>> records = new ArrayList<>();
        for (AttendanceRecord attendanceRecord : attendanceRecords) {
            Map<String, String> record = new LinkedHashMap<>();
            int id = attendanceRecord.getEmployee().getId();
            record.put("id",String.valueOf(id));
            String name = attendanceRecord.getEmployee().getName();
            record.put("name",name);
            Date datetime = attendanceRecord.getAccurateTime();
            String accurateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(datetime);
            record.put("time",accurateTime);
            records.add(record);
        }
        return records;
    }

    @Override
    public List<Map<String,String>> getAbsenceDayList(String day) {
        List<Employee> employees = attendanceRecordMapper.selectAbsenceDay(day);
        ArrayList<Map<String, String>> records = new ArrayList<>();
        for (Employee employee : employees) {
            Map<String, String> record = new LinkedHashMap<>();
            record.put("id",String.valueOf(employee.getId()));
            record.put("name",employee.getName());
            records.add(record);
        }
        return records;
    }

    @Override
    public List<Map<String, String>> getDayList(String day, Page page) {
        List<AttendanceRecord> attendanceRecords = attendanceRecordMapper.selectDay(day, page);
        ArrayList<Map<String, String>> records = new ArrayList<>();
        for (AttendanceRecord attendanceRecord : attendanceRecords) {
            Map<String, String> record = new HashMap<>();
            int id = attendanceRecord.getEmployee().getId();
            record.put("id",String.valueOf(id));
            String name = attendanceRecord.getEmployee().getName();
            record.put("name",name);
            Date datetime = attendanceRecord.getAccurateTime();
            String accurateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(datetime);
            record.put("time",accurateTime);
            records.add(record);
        }
        return records;
    }

    @Override
    public List<Map<String, String>> getAbsenceDayList(String day, Page page) {
        List<Employee> employees = attendanceRecordMapper.selectAbsenceDay(day, page);
        ArrayList<Map<String, String>> records = new ArrayList<>();
        for (Employee employee : employees) {
            Map<String, String> record = new HashMap<>();
            record.put("id",String.valueOf(employee.getId()));
            record.put("name",employee.getName());
            records.add(record);
        }
        return records;
    }

    @Override
    public List<Map<String, String>> getMonthList(String month) {
        List<Employee> employees = employeeMapper.getAll();
        ArrayList<Map<String, String>> records = new ArrayList<>();
        for (Employee employee : employees) {
            Map<String, String> record = new LinkedHashMap<>();
            int id = employee.getId();
            record.put("id",String.valueOf(id));
            String name = employee.getName();
            record.put("name",name);
            int attendance = attendanceRecordMapper.countOneMonth(employee.getId(),month);
            int countDay = DateUtil.getCountDay(month);
            double attendanceRate = (double)(Math.round(attendance * 1.0 / countDay * 100) / 100.0);
            record.put("attendanceRate",String.valueOf(attendanceRate));
            record.put("attendanceDay",String.valueOf(attendance));
            record.put("absenceDay",String.valueOf(countDay - attendance));
            records.add(record);
        }
        return records;
    }

    @Override
    public List<Map<String, String>> getMonthLateList(String month, String time) {
        List<Employee> employees = employeeMapper.getAll();
        ArrayList<Map<String, String>> records = new ArrayList<>();
        for (Employee employee : employees) {
            int lateDay = DateUtil.getCountDay(month) - attendanceRecordMapper.countEarlyOneMonth(employee.getId(),month,time);
            if(lateDay > 0){
                Map<String, String> record = new LinkedHashMap<>();
                int id = employee.getId();
                record.put("id",String.valueOf(id));
                record.put("name",String.valueOf(employee.getName()));
                record.put("lateDay",String.valueOf(lateDay));
                records.add(record);
            }
        }
        return records;
    }

    @Override
    public List<Map<String, String>> getMonthList(String month, Page page) {
        List<Employee> employees = employeeMapper.getAll(page);
        ArrayList<Map<String, String>> records = new ArrayList<>();
        for (Employee employee : employees) {
            Map<String, String> record = new HashMap<>();
            int id = employee.getId();
            record.put("id",String.valueOf(id));
            String name = employee.getName();
            record.put("name",name);
            int attendance = attendanceRecordMapper.countOneMonth(employee.getId(),month);
            int countDay = DateUtil.getCountDay(month);
            double attendanceRate = (double)(Math.round(attendance * 1.0 / countDay * 100) / 100.0);
            record.put("attendanceRate",String.valueOf(attendanceRate));
            record.put("attendanceDay",String.valueOf(attendance));
            record.put("absenceDay",String.valueOf(countDay - attendance));
            records.add(record);
        }
        return records;
    }

    @Override
    public List<Map<String, String>> getMonthLateList(String month, String time, Page page) {
        List<Employee> employees = employeeMapper.getAll(page);
        ArrayList<Map<String, String>> records = new ArrayList<>();
        for (Employee employee : employees) {
            int lateDay = DateUtil.getCountDay(month) - attendanceRecordMapper.countEarlyOneMonth(employee.getId(),month,time);
            if(lateDay > 0){
                Map<String, String> record = new HashMap<>();
                int id = employee.getId();
                record.put("id",String.valueOf(id));
                record.put("name",String.valueOf(employee.getName()));
                record.put("lateDay",String.valueOf(lateDay));
                records.add(record);
            }
        }
        return records;
    }

    @Override
    public Map<String, String> getOneDay(String day, int eid) {
        AttendanceRecord attendanceRecord = attendanceRecordMapper.selectOneDay(eid,day);
        if(attendanceRecord != null){
            Map<String, String> record = new LinkedHashMap<>();
            int id = attendanceRecord.getEmployee().getId();
            record.put("id",String.valueOf(id));
            String name = attendanceRecord.getEmployee().getName();
            record.put("name",name);
            Date datetime = attendanceRecord.getAccurateTime();
            String accurateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(datetime);
            record.put("time",accurateTime);
            return record;
        }
        return null;
    }

    @Override
    public List<Map<String, String>> getOneMonthList(String month, int eid) {
        List<AttendanceRecord> attendanceRecords = attendanceRecordMapper.selectOneMonth(eid, month);
        ArrayList<Map<String, String>> records = new ArrayList<>();
        for (AttendanceRecord attendanceRecord : attendanceRecords) {
            Map<String, String> record = new LinkedHashMap<>();
            record.put("id",String.valueOf(eid));
            String name = attendanceRecord.getEmployee().getName();
            record.put("name",name);
            Date datetime = attendanceRecord.getAccurateTime();
            String accurateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(datetime);
            record.put("time",accurateTime);
            records.add(record);
        }
        return records;
    }

    @Override
    public Map<String, String> getOneMonth(String month, int eid) {
//        List<AttendanceRecord> attendanceRecords = attendanceRecordMapper.selectOneMonth(eid, month);
        Map<String, String> record = new LinkedHashMap<>();
        Employee employee = employeeMapper.getById(eid);
        record.put("id",String.valueOf(eid));
        String name = employee.getName();
        record.put("name",name);
        int attendance = attendanceRecordMapper.countOneMonth(employee.getId(),month);
        int countDay = DateUtil.getCountDay(month);
        double attendanceRate = (double)(Math.round(attendance * 1.0 / countDay * 100) / 100.0);
        record.put("attendanceRate",String.valueOf(attendanceRate));
        record.put("attendanceDay",String.valueOf(attendance));
        record.put("absenceDay",String.valueOf(countDay - attendance));
        return record;
    }

    @Override
    public int countByDate(int id, String date) {
        return attendanceRecordMapper.countByDate(id, date);
    }

    @Override
    public void insertOneRecord(int id, String accurate, String date) {
        attendanceRecordMapper.insertOneRecord(id, accurate, date);
    }
}
